package me.ivenapp.gomoku;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.utils.net.Uri;

public class IntentBuilder {
    protected Context mContext;
    protected final Intent mIntent;
    protected final Intent.OperationBuilder operationBuilder;
    protected boolean hasBundleName;
    protected boolean hasAction;

    public IntentBuilder(Context context) {
        this(context, null);
    }

    public IntentBuilder(Context context, Intent intent) {
        mContext = context;
        operationBuilder = new Intent.OperationBuilder();
        if(intent == null) {
            mIntent = new Intent();
        } else {
            mIntent = intent;
        }
    }

    public IntentBuilder withDeviceId(String deviceId) {
        operationBuilder.withDeviceId(deviceId);
        return this;
    }

    public IntentBuilder withAbility(Class<?> cls) {
        operationBuilder.withAbilityName(cls);
        return this;
    }

    public IntentBuilder withAbility(String abilityName) {
        operationBuilder.withAbilityName(abilityName);
        return this;
    }

    /**
     * 没有则默认为本应用包名
     * @param bundleName
     * @return
     */
    public IntentBuilder withBundleName(String bundleName) {
        if(bundleName != null && !"".equals(bundleName)) {
            hasBundleName = true;
            operationBuilder.withBundleName(bundleName);
        }

        return this;
    }

    public IntentBuilder withAction(String action) {
        if(action != null && !"".equals(action)) {
            hasAction = true;
            operationBuilder.withAction(action);
        }
        return this;
    }

    public IntentBuilder withUri(Uri uri) {
        operationBuilder.withUri(uri);
        return this;
    }

    public IntentBuilder withFlags(int flags) {
        operationBuilder.withFlags(flags);
        return this;
    }

    public IntentBuilder setParam(String key, int value) {
        mIntent.setParam(key, value);
        return this;
    }

    public IntentBuilder setParam(String key, long value) {
        mIntent.setParam(key, value);
        return this;
    }

    public IntentBuilder setParam(String key, boolean value) {
        mIntent.setParam(key, value);
        return this;
    }

    public IntentBuilder setParam(String key, String value) {
        mIntent.setParam(key, value);
        return this;
    }

    /**
     * 获取构建好的Intent
     * @return
     */
    public Intent getBuildIntent(){
        mIntent.setOperation(operationBuilder.build());
        return mIntent;
    }

    public void goAbility() {
        mIntent.setOperation(operationBuilder.build());
        mContext.startAbility(mIntent, 0);
    }

    /**
     * context必须传入AbilitySlice
     */
    public void goAbilityForResult(int requestCode) {
        mIntent.setOperation(operationBuilder.build());
        if(mContext instanceof AbilitySlice) {
            ((AbilitySlice) mContext).startAbilityForResult(mIntent, requestCode);
        }
    }

    public void stopAbility() {
        mIntent.setOperation(operationBuilder.build());
        mContext.stopAbility(mIntent);
    }

    public void goPickImage(AbilitySlice slice, int requestCode) {
        mContext = slice;
        withAction("android.intent.action.PICK");
        mIntent.setType("image/*");
        goAbilityForResult(requestCode);
    }

    /**
     * 跳转到开启“修改系统设置”权限页面
     */
    public void goModifySettingPermission() {
        withAction("android.settings.action.MANAGE_WRITE_SETTINGS");
        withUri(Uri.parse("package:" + mContext.getBundleName()));
        goAbility();
    }
}
